import {config} from '../config.js';
// 错误码
const tips ={
  1:'抱歉,出现了一个错误',
  1005:'appkey无效',
  3000:'期刊不存在'
}
class HTTP{
  request({url,data,method='GET'}){
    return new Promise((resolve,reject)=>{
      this._request(resolve,reject,url,data,method)
    })
  }
    _request(resolve,reject,url,data={},method='GET'){
      wx.request({
        url: config.api_base_url + url,
        method:method,
        data:data,
        header:{
          'content-type':'application/json',
          'appkey':config.appkey
        },
        success:(res=>{
          let code = res.statusCode.toString()
          // startWith 判断开头是不是有2
          if(code.startsWith('2')){
            resolve(res.data)
          }else{
            reject()
            let error_code = res.data.error_code
            this._show_error(error_code)
          }
        }),
        fail:(err=>{
          reject()
          this._show_error(1)
        })
      })
    }

    _show_error(error_code){
      wx.showToast({
        title: tips[error_code]?tips[error_code]:tips[1],
        icon:'none',  
        duration:2000 
      })
    }
}
export {HTTP};